package com.lianyu.wangyiyun.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.lianyu.wangyiyun.R;


/**
 * @author Chen Yuguang
 */
public class CustomProgress extends View {

    /**
     * 画实心圆的画笔
     */
    private Paint mCirclePaint;
    /**
     * 画圆环的画笔
     */
    private Paint mRingPaint;
    /**
     * 画方形的画笔
     */
    private Paint mRectPaint;
    /**
     * 圆形颜色
     */
    private int mCircleColor;
    /**
     * 圆环颜色
     */
    private int mRingColor;
    /**
     * 半径
     */
    private float mRadius;
    /**
     * 圆环半径
     */
    private float mRingRadius;
    /**
     * 圆环宽度
     */
    private float mStrokeWidth;
    /**
     * 总进度
     */
    private int mTotalProgress;
    /**
     * 当前进度
     */
    private int mProgress;
    /**
     * 大圆
     */
    private Paint mBigPatint;
    /**
     * 外圆颜色
     */
    private int mBigCircleColor;
    /**
     * 方形颜色
     */
    private int mRectColor;

    private final RectF oval = new RectF();
    private final Path path = new Path();

    public static final int PLAY_STATUS = 0;
    public static final int PAUSE_STATUS = 1;

    public CustomProgress(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 获取自定义的属性
        initAttrs(context, attrs);
        initVariable();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typeArray = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.CustomProgress, 0, 0);
        mRadius = typeArray.getDimension(R.styleable.CustomProgress_radius, 300);
        mStrokeWidth = typeArray.getDimension(R.styleable.CustomProgress_strokeWidth, 20);
        mCircleColor = typeArray.getColor(R.styleable.CustomProgress_circleColor, Color.WHITE);
        mRingColor = typeArray.getColor(R.styleable.CustomProgress_ringColor, Color.RED);
        mTotalProgress = typeArray.getInt(R.styleable.CustomProgress_totalProgress, 100);
        mBigCircleColor = typeArray.getColor(R.styleable.CustomProgress_bigCircleColor, Color.BLACK);
        mRectColor = typeArray.getColor(R.styleable.CustomProgress_rectColor, Color.BLACK);
        typeArray.recycle();//注意这里要释放掉
        mRingRadius = mRadius + mStrokeWidth / 2;
    }

    /**
     * 初始化画笔
     */
    private void initVariable() {
        mCirclePaint = new Paint();
        mCirclePaint.setAntiAlias(true);
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setStrokeCap(Paint.Cap.ROUND);
        mCirclePaint.setStyle(Paint.Style.FILL);

        mRingPaint = new Paint();
        mRingPaint.setAntiAlias(true);
        mRingPaint.setColor(mRingColor);
        mRingPaint.setStrokeCap(Paint.Cap.ROUND);
        mRingPaint.setStyle(Paint.Style.STROKE);
        mRingPaint.setStrokeWidth(mStrokeWidth / 2);

        mBigPatint = new Paint();
        mBigPatint.setAntiAlias(true);
        mBigPatint.setColor(mBigCircleColor);
        mBigPatint.setStrokeCap(Paint.Cap.ROUND);
        mBigPatint.setStyle(Paint.Style.STROKE);
        mBigPatint.setStrokeWidth(mStrokeWidth / 2);

        mRectPaint = new Paint();
        mRectPaint.setAntiAlias(true);
        mRectPaint.setStyle(Paint.Style.FILL);
        mRectPaint.setColor(mRectColor);
        mRectPaint.setStrokeWidth(mStrokeWidth);
    }

    private boolean isPlay = false;

    @Override
    protected void onDraw(Canvas canvas) {
        //计算中心点
        int mXCenter = getWidth() / 2;
        int mYCenter = getHeight() / 2;
        //正在播放状态或者暂停状态的时候 画圆环
        canvas.drawCircle(mXCenter, mYCenter, mRadius + mStrokeWidth / 2, mBigPatint);
        canvas.drawCircle(mXCenter, mYCenter, mRadius, mCirclePaint);
        //判断是正在播放还是暂停状态
        if (isPlay) {
            //正在播放状态，画出中间双竖
            canvas.drawLine(mXCenter - mRadius / 4, mYCenter - mRadius / 2 - mStrokeWidth / 4, mXCenter - mRadius / 4, mYCenter + mRadius / 2 + mStrokeWidth / 4, mRectPaint);
            canvas.drawLine(mXCenter + mRadius / 4, mYCenter - mRadius / 2 - mStrokeWidth / 4, mXCenter + mRadius / 4, mYCenter + mRadius / 2 + mStrokeWidth / 4, mRectPaint);
        } else {
            //暂停播放状态，画三角形
            path.reset();
            path.moveTo(mXCenter - mRadius / 4, mYCenter - mRadius / 2 - mStrokeWidth / 5);
            path.lineTo(mXCenter - mRadius / 4, mYCenter + mRadius / 2 + mStrokeWidth / 5);
            path.lineTo(mXCenter + mRadius / 2 + mStrokeWidth / 5, mYCenter);
            path.lineTo(mXCenter - mRadius / 4, mYCenter - mRadius / 2);
            path.close();
            canvas.drawPath(path,mRectPaint);
        }
        //根据进度画圆弧
        if (mProgress > 0) {
            oval.left = (mXCenter - mRingRadius);
            oval.top = (mYCenter - mRingRadius);
            oval.right = mRingRadius * 2 + (mXCenter - mRingRadius);
            oval.bottom = mRingRadius * 2 + (mYCenter - mRingRadius);
            canvas.drawArc(oval, -90, ((float) mProgress / mTotalProgress) * 360, false, mRingPaint);
        }
    }

    /**
     * 设置进度的方法
     *
     * @param progress 进度
     * @author Chen Yuguang
     * @date 2021/5/9 5:38
     */
    public void setProgress(int progress) {
        mProgress = progress;
        postInvalidate();
    }

    /**
     * 设置总进度
     *
     * @param totalProgress 总进度
     * @author Chen Yuguang
     * @date 2021/5/9 5:39
     */
    public void setmTotalProgress(int totalProgress) {
        mTotalProgress = totalProgress;
    }

    /**
     * 设置当前的按钮的播放状态
     *
     * @param status 状态（0为播放状态，1为暂停状态）
     * @author Chen Yuguang
     * @date 2021/5/9 5:39
     */
    public void setStatus(int status) {
        switch (status) {
            case PLAY_STATUS:
                if (!isPlay) {
                    isPlay = true;
                    postInvalidate();
                }
                break;
            case PAUSE_STATUS:
                if (isPlay) {
                    isPlay = false;
                    postInvalidate();
                }
                break;
            default:
        }
    }

    /**
     * 设置中间双竖颜色的方法
     *
     * @param mRectColor 中间双竖颜色
     * @author Chen Yuguang
     * @date 2021/5/9 5:43
     */
    public void setRectColor(int mRectColor) {
        this.mRectColor = mRectColor;
        mRectPaint.setColor(mRectColor);
        postInvalidate();
    }

    /**
     * 设置内部实心圆颜色的方法
     *
     * @param mCircleColor 内部实心圆颜色
     * @author Chen Yuguang
     * @date 2021/5/9 5:43
     */
    public void setCircleColor(int mCircleColor) {
        this.mCircleColor = mCircleColor;
        mCirclePaint.setColor(mCircleColor);
        postInvalidate();
    }

    /**
     * 设置进度条颜色的方法
     *
     * @param mRingColor 进度条颜色
     * @author Chen Yuguang
     * @date 2021/5/9 5:43
     */
    public void setRingColor(int mRingColor) {
        this.mRingColor = mRingColor;
        mRingPaint.setColor(mRingColor);
        postInvalidate();
    }

    /**
     * 设置外部空心圆颜色的方法
     *
     * @param mBigCircleColor 外部空心圆颜色
     * @author Chen Yuguang
     * @date 2021/5/9 5:43
     */
    public void setBigCircleColor(int mBigCircleColor) {
        this.mBigCircleColor = mBigCircleColor;
        mBigPatint.setColor(mBigCircleColor);
        postInvalidate();
    }

}